package com.itheima.tliasmanagement.controller;

import com.itheima.tliasmanagement.dto.AddEmpDto;
import com.itheima.tliasmanagement.dto.EmpListDTO;
import com.itheima.tliasmanagement.dto.UpdateEmpDto;
import com.itheima.tliasmanagement.entity.Emp;
import com.itheima.tliasmanagement.service.EmpService;
import com.itheima.tliasmanagement.vo.EmpListVo;
import com.itheima.tliasmanagement.vo.EmpVo;
import com.itheima.tliasmanagement.vo.PageBean;
import com.itheima.tliasmanagement.vo.Result2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工管理的接口
 * */
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 员工列表分页查询
     *
     * */
    @GetMapping
    @CrossOrigin(origins = "http://127.0.0.1:5173")
    public Result2<PageBean<EmpListVo>> emps(EmpListDTO dto, HttpServletResponse response){
        PageBean<EmpListVo> pageBean = empService.emps(dto);
//        response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500");
//        response.addHeader("Access-Control-Allow-Methods", "*");
//        response.addHeader("Access-Control-Allow-Headers", "*");
        return Result2.ok(pageBean);
    }

    /**
     * 新增员工
     * */
    @PostMapping
    public Result2 addEmp(@RequestBody AddEmpDto dto){
        empService.addEmp(dto);
        return Result2.ok();
    }

    /**
     * 批量删除
     * */
    @DeleteMapping
    public Result2 deleteEmps(@RequestParam("ids")List<Integer>ids){
    // public Result2 deleteEmps(Integer[] ids){
        empService.deleteEmps(ids);
        return Result2.ok();
    }

    /**
     * 根据id查询员工信息，包含了员工的基本信息和工作经历信息
     * */
    @GetMapping("{id}")
    public Result2<EmpVo> getById(@PathVariable("id")Integer id){
        EmpVo vo = empService.getById(id);
        return Result2.ok(vo);
    }

    /**
     * 更新员工信息
     * */
    @PutMapping
    public Result2 updateEmp(@RequestBody UpdateEmpDto dto){
        empService.updateEmp(dto);
        return Result2.ok();
    }

    /**
     * 查询所有员工
     * */
    @GetMapping("/list")
    public Result2<List<Emp>> all(){
        List<Emp> list = empService.all();
        return Result2.ok(list);
    }




}
